package com.atguigu.myjuc.block;

import java.util.concurrent.*;

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(3);//SynchronousQueue<>();//new ArrayBlockingQueue(3);
                /*
                *ArrBlockingQueue由数组组成的有界阻塞队列
                *LinkedBlockingQueue由链表组成的有届阻塞队列 大小默认值为Integer.MAX_VALUE）
                *SynchronousQueue不存储元素的阻塞队列，也即单个元素的队列。
                * */

        /*
        * add添加  remove移除  element检查
        * 抛出异常
        *
        * 当阻塞队列满时，再往队列里add插入元素会抛IllegalStateException:Queue full
          当阻塞队列空时，再往队列里remove移除元素会抛NoSuchElementException
        * */


        /*blockingQueue.add("A");
        blockingQueue.add("B");
        blockingQueue.add("C");
        //blockingQueue.add("D");    //add remove超出长度会抛异常
        System.out.println(blockingQueue.element());    //获取首个
        System.out.println("blockingQueue = " + blockingQueue);
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        blockingQueue.add("D");
        System.out.println(blockingQueue.remove());
        System.out.println("blockingQueue = " + blockingQueue);*/


        /*
        *           offer插入  poll移除  peek检查
        *  返回状态值  true/      null
        *
        * 插入方法，成功ture失败false
          移除方法，成功返回出队列的元素，队列里没有就返回null
        * */

        /*System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.offer("d"));//获取首个
        System.out.println(blockingQueue.peek());
        System.out.println("blockingQueue = " + blockingQueue);
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());*/


        /*
        * 一直阻塞
        *
        * 当阻塞队列满时，生产者线程继续往队列里put元素，队列会一直阻塞生产者线程直到put数据or响应中断退出
          当阻塞队列空时，消费者线程试图从队列里take元素，队列会一直阻塞消费者线程直到队列可用
        *
        * put 添加  take 移除 没有检查
        * */

       /* blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //blockingQueue.put("d");
        System.out.println("blockingQueue = " + blockingQueue);
        System.out.println(blockingQueue.size());
        blockingQueue.take();
        blockingQueue.take();
        blockingQueue.take();
        //blockingQueue.take();
        System.out.println("blockingQueue = " + blockingQueue);
        System.out.println(blockingQueue.size());
*/


        /*超时退出
        *
        *当阻塞队列满时，队列会阻塞生产者线程一定时间，超过限时后生产者线程会退出
        *
        * offer（ 值，时间，单位 ）添加    poll（ 时间，单位  ）移除   没有检查
         * */

        blockingQueue.offer("a",1, TimeUnit.SECONDS);
        blockingQueue.offer("b",1, TimeUnit.SECONDS);
        blockingQueue.offer("c",1, TimeUnit.SECONDS);
        blockingQueue.offer("d",1, TimeUnit.SECONDS);
        System.out.println("blockingQueue = " + blockingQueue);
        blockingQueue.poll(1,TimeUnit.SECONDS);
        blockingQueue.poll(1,TimeUnit.SECONDS);
        blockingQueue.poll(1,TimeUnit.SECONDS);
        blockingQueue.poll(1,TimeUnit.SECONDS);
        blockingQueue.offer("d",1, TimeUnit.SECONDS);
        blockingQueue.offer("f",1, TimeUnit.SECONDS);
        System.out.println("blockingQueue = " + blockingQueue);




    }
}
